package com.lzy.imagepicker.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.R;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.util.Utils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImageAndVideoPageAdapter extends PagerAdapter {

    private int screenWidth;
    private int screenHeight;
    private ImagePicker imagePicker;
    private ArrayList<ImageItem> images = new ArrayList<>();
    private Activity mActivity;
    private StandardGSYVideoPlayer mVideoPlayer;


    public ImageAndVideoPageAdapter(Activity activity, ArrayList<ImageItem> images) {
        this.mActivity = activity;
        this.images = images;

        DisplayMetrics dm = Utils.getScreenPix(activity);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        imagePicker = ImagePicker.getInstance();
    }

    public void setData(ArrayList<ImageItem> images) {
        this.images = images;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageItem imageItem = images.get(position);

        if (imageItem.isVideo) {
            View view = View.inflate(mActivity, R.layout.layout_video, null);
            StandardGSYVideoPlayer standardGSYVideoPlayer = view.findViewById(R.id.video_player_2);
            mVideoPlayer = standardGSYVideoPlayer;
            standardGSYVideoPlayer.getBackButton().setVisibility(View.GONE);
            standardGSYVideoPlayer.getFullscreenButton().setVisibility(View.GONE);
            if (!TextUtils.isEmpty(imageItem.path)) {
                standardGSYVideoPlayer.setUp(imageItem.path, false, null);
            } else if (!TextUtils.isEmpty(imageItem.url)) {
                standardGSYVideoPlayer.setUp(imageItem.url, false, null);//todo 缓存的视频不能播放

            }

            container.addView(view);
            return view;
        } else {

            PhotoView photoView = new PhotoView(mActivity);
            if (!TextUtils.isEmpty(imageItem.url)) {
                String url = imageItem.url;
                imagePicker.getImageLoader().displayImage(mActivity,url,photoView);
            } else {
                if (imageItem.path != null) {

                    imagePicker.getImageLoader().displayImage(mActivity, imageItem.path,photoView);
                }
            }

            //            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            //                @Override
            //                public void onPhotoTap(View view, float x, float y) {
            //                    if (photoListener != null)
            //                        photoListener.OnPhotoTapListener(view, x, y);
            //                }
            //            });
            container.addView(photoView);
            return photoView;

        }


    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    public StandardGSYVideoPlayer getVideoPlayer() {
        return mVideoPlayer;
    }


    /**
     * 图片点击监听
     */
    //    public PhotoViewClickListener photoListener;
    //
    //    public void setPhotoViewClickListener(PhotoViewClickListener photoListener) {
    //        this.photoListener = photoListener;
    //    }
    //
    //    public interface PhotoViewClickListener {
    //        void OnPhotoTapListener(View view, float v, float v1);
    //    }
}
